package wechat.rcv.controller

import org.springframework.beans.factory.annotation.Autowired
import edu.fudan.mylang.pf.IObjectFactory
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.ui.Model
import org.apache.log4j.Logger
import org.springframework.web.bind.annotation.RequestMapping
import com.shrcl.engineerprocess.domain.CorpWeChatUserDAO
import com.shrcl.engineerprocess.domain.NotificationCriteria
import com.shrcl.engineerprocess.domain.TrainingNotification;
import com.shrcl.engineerprocess.domain.EngineerProjectInitNotification
import com.shrcl.engineerprocess.domain.CertificationNotification
import wechat.app.UserID
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RequestMethod
import org.json.JSONObject
import java.text.SimpleDateFormat
import com.shrcl.engineerprocess.domain.Notification
import java.util.ArrayList
import java.util.Collection

import org.apache.commons.collections.IteratorUtils;
import java.util.List
import com.shrcl.engineerprocess.domain.NotificationStatus

@RestController
@Transactional
@ResponseBody
@RequestMapping("/mynotification")
class GetMyNotification {

	final static Logger logger = Logger.getLogger(typeof(GetMyNotification).getName());

	@Autowired IObjectFactory entities;
	@Autowired CorpWeChatUserDAO corpWeChatUserDAO

	@RequestMapping(method = RequestMethod::POST,  produces = "application/json;charset=UTF-8" )
	def ArrayWapper process(String wechatid, Integer page, Integer pagesize) {
		logger.info('=========>GetMyNotification.processs<============')
		logger.info('=========>GetMyNotification.wechatid<============'+wechatid)
		logger.info('=========>GetMyNotification.page<============'+page)
		logger.info('=========>GetMyNotification.pagesize<============'+pagesize)
		try{
			val receiver = corpWeChatUserDAO.findWeChatUser(wechatid)?.engineer.asNotifyReceiver
			val c = (entities.createQueryCriteria(typeof(NotificationCriteria))=>[
				it.receiver = receiver
				it.^start = (page - 1) * pagesize
				it.^limit = pagesize 
			])
			
			val ret = new ArrayWapper
			val result = c.list.^map[n|
				val notiType = (n as com.shrcl.engineerprocess.domain.Notification).getNotificationType
				new wechat.rcv.controller.WXNotification=>[
					it.id = n.getId
					it.date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(n.getNotifyDate)
					it.getWeekDay(n.getNotifyDate.getDay)
					if (n.getStatus == NotificationStatus.confirmed) {
						it.isFinished = true
					} else it.isFinished = false
					println("it.id====>>>>"+it.id)
					println("it.title====>>>>"+it.title)
					println("it.date====>>>>"+it.date)
					println("NotificationStatus====>>>>"+n.getStatus)
					if (notiType == "TrainingNotification") {
							val nd = (n as TrainingNotification).getEngineerTraining
							it.title = nd.getName()
					} else if (notiType == "EngineerProjectInitNotification") {
							val nd = (n as EngineerProjectInitNotification).getEngineerCostEstimation
							it.title = nd.getProject().getName()
					} else if(notiType == "CertificationNotification") {
							val nd = (n as CertificationNotification).getEngineerCertification
							it.title = nd.getCertification().getName()
					}
				]
			]
			ret.result = IteratorUtils::toList(result.iterator);     
			ret.count = ret.result.size
			println("*******GetMyNotification.result.size******"+ret.count)
			return ret
		}catch(Exception e){
			e.printStackTrace
			null
		}
	}

}


class ArrayWapper{
	public List<WXNotification> result;
	public Integer count;
}